﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace QLK.Database.DALs
{
    public class Storage_ProductDAL
    {
        private ManageStoreDataContext db;

        public Storage_ProductDAL()
        {
            db = new ManageStoreDataContext();
        }

        public List<Storage_Product> Storage_ProductGetAll()
        {
            var SPs = from sp in db.Storage_Products
                      orderby sp.ProductID
                      select sp;

            return SPs.ToList<Storage_Product>();
        }

        public Storage_Product Storage_ProductGetByID(int StorageID, int ProductID)
        {
            Storage_Product SPs = db.Storage_Products.Single<Storage_Product>(sp => sp.StorageID == StorageID
                && sp.ProductID == ProductID);

            return SPs;
        }

        public bool Storage_ProductInsert(Storage_Product sps)
        {
            try
            {
                db.Storage_Products.InsertOnSubmit(sps);
                db.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return false;
            }
        }

        public bool Storage_ProductUpdate(Storage_Product sps)
        {
            Storage_Product SPs = db.Storage_Products.Single<Storage_Product>(sp => sp.StorageID == sps.StorageID
                && sp.ProductID == sps.ProductID);

            SPs.Quantity = sps.Quantity;

            try
            {
                db.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return false;
            }
        }

        public bool Storage_ProductDelete(int StorageID, int ProductID)
        {
            Storage_Product SPs = db.Storage_Products.Single<Storage_Product>(sp => sp.StorageID == StorageID
                && sp.ProductID == ProductID);

            if (SPs == null)
                return false;

            db.Storage_Products.DeleteOnSubmit(SPs);

            try
            {
                db.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return false;
            }
        }

        public bool Storage_ProductCheckDuplicateProduct(int StorageID, int ProductID)
        {
            var SP = from s in db.Storage_Products
                      select s;
            if (SP.ToList<Storage_Product>().Count == 0)
                return true;
            else
            {
                Storage_Product SPs = db.Storage_Products.Single<Storage_Product>(sp => sp.StorageID == StorageID
                    && sp.ProductID == ProductID);

                if (SPs == null)
                    return true;
                return false;
            }
        }
    }
}
